package com.ihr360.payroll.service.salarytasklet.convertor;

import com.ihr360.commons.lang.CollectionUtils;
import com.ihr360.commons.model.IConvertor;
import com.ihr360.payroll.calc.StaffSalaryCalcPeriod;
import com.ihr360.payroll.calc.StaffSalaryCalcPeriodTerm;
import com.ihr360.payroll.model.dto.salary.task.period.StaffPayPeriodTermSalary;
import com.ihr360.payroll.model.dto.salary.task.period.embeddable.StaffSalarySnapshot;
import com.ihr360.payroll.model.dto.salary.task.period.embeddable.StaffSalarySnapshotListWrapper;
import com.ihr360.payroll.model.dto.salary.task.period.embeddable.StaffSalaryTermId;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;
/**
 * @author davidwei
 */
@Component
public class StaffPayPeriodSalaryConvertor implements IConvertor<StaffSalaryCalcPeriod, StaffPayPeriodTermSalary> {
    @Override
    public StaffPayPeriodTermSalary convertEntityToDto(StaffSalaryCalcPeriod entity) {
        StaffPayPeriodTermSalary dto = new StaffPayPeriodTermSalary();
        BeanUtils.copyProperties(entity, dto);
        dto.setId(new StaffSalaryTermId(entity.getTermId(), entity.getStaffId()));
        StaffSalarySnapshot summarizedData = new StaffSalarySnapshot();
        summarizedData.setData(entity.getSummarizedData());
        summarizedData.setStartOn(entity.getStartOn());
        summarizedData.setEndOn(entity.getEndOn());

        dto.setSummarizedData(summarizedData);

        StaffSalarySnapshotListWrapper phasedData = new StaffSalarySnapshotListWrapper();
        if (CollectionUtils.isNotEmpty(entity.getPhases())) {
            List<StaffSalarySnapshot> phasedDataList = entity.getPhases().stream().map(s -> {
                StaffSalarySnapshot salarySnapshot = new StaffSalarySnapshot();
                salarySnapshot.setData(s.getItems());
                salarySnapshot.setStartOn(s.getStartOn());
                salarySnapshot.setEndOn(s.getEndOn());
                return salarySnapshot;
            }).collect(Collectors.toList());
            phasedData.setPhasedData(phasedDataList);
            dto.setPhasedData(phasedData);
            dto.setYear(entity.getYear());
            dto.setMonth(entity.getMonth());
        } else {
            dto.setPhasedData(null);
        }
        return dto;
    }

    @Override
    public StaffSalaryCalcPeriod convertDtoToEntity(StaffPayPeriodTermSalary dto) {
        StaffSalaryCalcPeriod entity = new StaffSalaryCalcPeriod();
        BeanUtils.copyProperties(dto, entity);
        StaffSalarySnapshot salarySnapshot = dto.getSummarizedData();
        if (salarySnapshot != null) {
            entity.setSummarizedData(salarySnapshot.getData());
        }
        StaffSalarySnapshotListWrapper phasedData = dto.getPhasedData();
        if (phasedData != null && CollectionUtils.isNotEmpty(phasedData.getPhasedData())) {
            List<StaffSalaryCalcPeriodTerm> terms = phasedData.getPhasedData().stream().map(sn -> {
                StaffSalaryCalcPeriodTerm term = new StaffSalaryCalcPeriodTerm();
                term.setItems(sn.getData());
                term.setStartOn(sn.getStartOn());
                term.setEndOn(sn.getEndOn());
                return term;
            }).collect(Collectors.toList());
            entity.setPhases(terms);
        }
        return entity;
    }
}